package com.yan.utils;


import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author 汪焰
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {


    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYYMMDD = "yyyyMMdd";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static String YYYY_MM_DD_00_00_00 = "yyyy-MM-dd 00:00:00";
    public static String YYYY_MM_DD_23_59_59 = "yyyy-MM-dd 23:59:59";

    private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss",
            "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    public static String START = " 00:00:00";
    public static String END = " 23:59:59";

    /**
     * date1 <= date2 返回true
     *
     * @Author: 汪焰
     * @Date: 2020/7/17 14:23
     * version 1.0.0
     **/
    public static boolean lte(Date date1, Date date2) {
        if (date1 == null && date2 == null) {
            return true;
        }
        if (date1 == null || date2 == null) {
            return false;
        }
        long time1 = date1.getTime();
        long time2 = date2.getTime();
        if (time1 <= time2) {
            return true;
        }
        return false;
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object obj) {
        if (obj == null) {
            return null;
        }
        try {
            return parseDate(obj.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    public static final String parseDateToStr(Date date, String format) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final String parseDateToStr(Date date) {
        return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(date);
    }

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getDate() {
        return new Date();
    }

    public static final String getDateStr() {
        return getDateStr(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String getDateStr(String format) {
        return parseDateToStr(getDate(), format);
    }

    /**
     * 加 n 年
     */
    public static Date addYear(Date date, int nYear) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, nYear);
        return calendar.getTime();
    }

    /**
     * 加 n 月
     */
    public static Date addMonth(Date date, int nMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, nMonth);
        return calendar.getTime();
    }

    /**
     * 加 n 周
     */
    public static Date addWeek(Date date, int nWeek) {
        return addDay(date, nWeek * 7);
    }

    /**
     * 加 n 天
     */
    public static Date addDay(Date date, int nDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, nDay);
        return calendar.getTime();
    }

    /**
     * 加 n 小时
     */
    public static Date addHour(Date date, int nHour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, nHour);
        return calendar.getTime();
    }

    /**
     * 加 n 分钟
     */
    public static Date addMinute(Date date, int nMin) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, nMin);
        return calendar.getTime();
    }

    /**
     * 加 n 秒
     */
    public static Date addSecond(Date date, int nSecond) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, nSecond);
        return calendar.getTime();
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }
}